//
// Created by wsl on 2024/3/9.
//

#include "SqQueue.h"

DataStructure_Warning_Unused
SqQueue SqQueue_create(size_t dataSize, uin32_t initSize)
{
	if (initSize < 1)
		return NULL;

	//创建栈对象
	SqQueue queue = DataStructure_AllocT(SqQu);
	if (NULL == queue)
	{
		DataStructure_Overflow_Handler();

		return NULL;
	}

	//创建元素空间
	T temp = DataStructure_Alloc(dataSize * initSize);
	if(NULL == temp)
	{
		DataStructure_MemoryFree(queue);

		DataStructure_Overflow_Handler();

		return NULL;
	}

	//初始化对象
	queue->dataSize = dataSize;
	queue->capacity = initSize;
	queue->base = temp;
	queue->length = 0;
	queue->front = 0;
	queue->rear = 0;

	//返回对象
	return queue;
}

DataStructure_Warning_Unused
Status SqQueue_isEmpty(SqQueue queue)
{
	if (NULL == queue)
		return TRUE;

	return (queue->length <= 0 ? TRUE : FALSE);
}

DataStructure_Warning_Unused
Status SqQueue_enQueue(SqQueue queue, T e)          //入队
{
	//空指针，或队满
	if (NULL == queue || queue->length >= queue->capacity)
		return FALSE;

	//将数据拷贝到队中
	DataStructure_MemoryCpy((uint8_t*)queue->base + queue->rear * queue->dataSize, e, queue->dataSize);

	queue->rear = (queue->rear + 1) % queue->capacity;			//队尾+1

	queue->length++;

	return TRUE;
}

DataStructure_Warning_Unused
Status SqQueue_deQueue(SqQueue queue, T buf)                    //出队，并返回出队的节点值
{
	//空指针，或队空
	if (SqQueue_isEmpty(queue))
		return FALSE;

	//将数据保存到buf中
	DataStructure_MemoryCpy(buf, (uint8_t*)queue->base + queue->front * queue->dataSize, queue->dataSize);

	queue->front = (queue->front + 1) % queue->capacity;

	queue->length--;
	return TRUE;
}

DataStructure_Warning_Unused
Status SqQueue_clear(SqQueue queue)         //清除队列
{
	if (SqQueue_isEmpty(queue))
		return FALSE;

	//前后索引复位，对象各属性复位
	queue->length = 0;
	queue->front = 0;
	queue->rear = 0;

	return TRUE;
}

DataStructure_Warning_Unused
Status SqQueue_destroy(SqQueue *queue)      //销毁队列
{
	if (NULL == queue || NULL == *queue)
		return FALSE;

	//释放容器
	DataStructure_MemoryFree((*queue)->base);

	//释放对象本身
	DataStructure_MemoryFree(*queue);

	*queue = NULL;

	return TRUE;
}

DataStructure_Warning_Unused
Status SqQueue_isFull(SqQueue queue)        //是否满
{
	if(NULL == queue)
		return FALSE;

	return queue->length >= queue->capacity ? TRUE : FALSE;
}

DataStructure_Warning_Unused
Status SqQueue_front(SqQueue queue, T buf)          //获取队头的值，但不出队
{
	if (SqQueue_isEmpty(queue))
		return FALSE;

	//将数据拷贝到队中
	DataStructure_MemoryCpy(buf, (uint8_t*)queue->base + queue->front * queue->dataSize, queue->dataSize);

	return TRUE;
}
